<?php

class Users_Profile_EditAction extends SrsUsersBaseAction
{
	public function getCredentials()
	{
		return 'users.profile';
	}

	public function executeRead(AgaviRequestDataHolder $rd)
	{
		return 'Input';
	}

	public function executeWrite(AgaviRequestDataHolder $rd)
	{
		$userData = $rd->getParameter('user');

		$currentUser = $this->getContext()->getUser()->getCurrentUser();

		$user = Doctrine_Query::create()
		        ->from('User')
			      ->where('id=?', $currentUser['id'])
					  ->fetchOne();

		if (!empty($userData['password']))
		{
			$user->password = hash('sha256', $userData['password']);
		}

		$user->email = $userData['email'];
		$user->name  = $userData['name'];
		$user->phone = $userData['phone'];

		try
		{
			$user->save();

			$this->getContext()->getUser()->setAttribute('user', $user);
		}
		catch (Exception $x)
		{
			$this->addError('email_taken', 'user[email]', 'Podany adres e-mail już jest wykorzystany.');

			return 'Error';
		}

		return 'Success';
	}
}